/*
    NOTE: PSID_incscores_bothparents is created in the 
          build/PSID/code/Actual_vs_predictedincome
          folder. Run 1_PSID and 2_PSID do files first.
*/

clear 
set more off


   use "$PSID/output/PSID_incscores_bothparents.dta", clear

    levelsof birthyear_bin5, local(levels) 
    di "`levels'"

*----------------------------------*
* REGRESSIONS
*----------------------------------*

*--------------------------
* WORKING DAD SAMPLE
*--------------------------

//(a) Actual dad income
   preserve 
      keep if analysis_samp_3yrs_workingdads==1 

      matrix ige_wkdad_actualinc= J(7,3,.)
      local i =0  

        foreach y in `levels'  {
            reg log_mean_totfaminc_3years log_mean_fathertotfaminc_3years if birthyear_bin5==`y' [w=weight1997], cluster(parent_id)
            
            local i=`i'+1
            matrix ige_wkdad_actualinc[`i',1] = `y'
            matrix ige_wkdad_actualinc[`i',2] = _b[log_mean_fathertotfaminc_3years]
            matrix ige_wkdad_actualinc[`i',3] = _se[log_mean_fathertotfaminc_3years]
        }      

   restore

//(b) Predicted dad income
   preserve 
      keep if analysis_samp_3yrs_workingdads==1 

      matrix ige_wkdad_predinc= J(7,3,.)
      local i =0  

        foreach y in `levels'  {
            reg log_mean_totfaminc_3years log_father_interpolated if birthyear_bin5==`y' [w=weight1997], cluster(parent_id)
            
            local i=`i'+1
            matrix ige_wkdad_predinc[`i',1] = `y'
            matrix ige_wkdad_predinc[`i',2] = _b[log_father_interpolated]
            matrix ige_wkdad_predinc[`i',3] = _se[log_father_interpolated]
        }      

   restore

*---------------------------------------------------------------------------*
*---------------------------------------------------------------------------*

*------------------------------------*
* FIGURE
*------------------------------------*
         
    //Working dads, actual vs predicted dad income
    clear 
    svmat ige_wkdad_actualinc
    ren ige_wkdad_actualinc1 bin
    gen l95_ige_wkdad_actualinc = ige_wkdad_actualinc2-1.96*ige_wkdad_actualinc3
    gen u95_ige_wkdad_actualinc = ige_wkdad_actualinc2+1.96*ige_wkdad_actualinc3
    sort bin
    tempfile ige_wkdad_actualinc
    save `ige_wkdad_actualinc'

    clear 
    svmat ige_wkdad_predinc
    ren ige_wkdad_predinc1 bin
    gen l95_ige_wkdad_predinc = ige_wkdad_predinc2-1.96*ige_wkdad_predinc3
    gen u95_ige_wkdad_predinc = ige_wkdad_predinc2+1.96*ige_wkdad_predinc3
    sort bin

    merge 1:1 bin using `ige_wkdad_actualinc'
    drop _merge

    gen bin2 = bin + 0.05

    preserve
        keep if bin<=3 //birth cohorts up to 1960
        #delimit ;
        twoway (connect ige_wkdad_actualinc2 bin, msymbol(circle_hollow) mcolor(pink*1.5) msize(medium) lcolor(pink*1.5) lwidth(0.25) lpat(solid)) (rcap u95_ige_wkdad_actualinc l95_ige_wkdad_actualinc bin, lcolor(pink*1.5) lwidth(0.25) lpat(solid))
               (connect ige_wkdad_predinc2 bin2, msymbol(square) mcolor(lavender) msize(medium) lcolor(lavender) lwidth(0.25) lpat(solid)) (rcap u95_ige_wkdad_predinc l95_ige_wkdad_predinc bin2, lcolor(lavender) lwidth(0.25) lpat(solid)),        
        graphregion(color(white)) legend(on size(small) ring(0) row(2) pos(5) order(1 "Actual father income" 3 "Predicted father income")) 
        xtitle(" " "Birth Year (binned)") ytitle("IGE" " ", axis(1)) ylabel(0(0.4)1.8,nogrid axis(1)) xscale(range(0.5 3.5))
        xlabel(1 "1946-1950" 2 "1951-1955" 3 "1956-1960", labsize(small)  angle(45))  
        ;
        #delimit cr
        graph export "$Mydirectory2/appendix_d/ige_workingdads_PSID_upto1960.pdf", as(pdf) replace
    restore

